<?php
class ModelMessageWeekSituation extends Model {
	public function getWeekSituations(){
		$query = $this->db->query("SELECT   mws.* FROM  ".DB_PREFIX."message_week_situation mws where status='1'");
		$result =  $query->rows;
		$situationData = array();
		if($result){
			foreach ($result as $week_situation) {
				$situationData[] = array(
					'name' => $week_situation['name'],
					'policeSituations' => $this->getPoliceSituations($week_situation['week_situation_id']),
				);
			}
		}
		
		return $situationData;
	}
	
	
	public function getPoliceSituations($week_situation_id){
		$query = $this->db->query("SELECT   * FROM  ".DB_PREFIX."message_situation_to_week mstw  left join  ".DB_PREFIX."message_police_situation_article mpsa on(mpsa.police_situation_id=mstw.police_situation_id) where mstw.week_situation_id='".(int)$week_situation_id."'");
		return  $query->rows;
	}
	
	public function getPoliceSituationss($data = array()){
		$sql = "SELECT   * FROM  ".DB_PREFIX."message_police_situation_article mstw  ";
		
		$sort_data = array(
			'date_added',
		);	

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}

		if (isset($data['order']) && ($data['order'] == 'ASC')) {
			$sql .= " ASC";
		} else {
			$sql .= " DESC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		
		$query = $this->db->query($sql);
		
		return  $query->rows;
	}
	
	public function getTotalPoliceSituationss($data = array()){
		$query = $this->db->query("SELECT   count(*) as total FROM  ".DB_PREFIX."message_police_situation_article mstw  ");
		return  $query->row['total'];
	}
	
	
	public function getPoliceSituationInfo($police_situation_id){
		$query = $this->db->query("SELECT   * FROM  ".DB_PREFIX."message_police_situation_article mpsa where mpsa.police_situation_id='".(int)$police_situation_id."'");
		return  $query->row;
	}
	
}
?>